package com.store.app.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Color;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.store.app.R;
import com.store.app.bean.CouponBean;
import com.store.app.utils.SettingData;

import java.util.List;


public class CouponAdapter extends BaseAdapter {
    private Context context;        //上下文对象引用
    private List<CouponBean> couponBeanList;
    private final String STATE_PENGDING = "pending";//待定
    private final String STATUS_ON_SHELF = "on_shelf";//上架
    private final String STATUS_OFF_SHELF = "off_shelf";//下架
    private final String STATUS_REJECT = "reject";//驳回

    public CouponAdapter(Context context, List<CouponBean> couponBeanList) {
        this.context = context;
        this.couponBeanList = couponBeanList;
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return couponBeanList.size();
    }


    @Override
    public CouponBean getItem(int position) {
        return couponBeanList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @SuppressLint("NewApi")
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        CouponBean couponBean = couponBeanList.get(position);
        ViewHolder viewHolder = null;
        if (convertView == null) {
            viewHolder = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.activity_coupon_listview_item, null);
            viewHolder.tv_rule = (TextView) convertView.findViewById(R.id.tv_rule);
            viewHolder.tv_issued_num = (TextView) convertView.findViewById(R.id.tv_issued_num);
            viewHolder.tv_use_num = (TextView) convertView.findViewById(R.id.tv_use_num);
            viewHolder.tv_verificatnum = (TextView) convertView.findViewById(R.id.tv_verificatnum);
            viewHolder.tv_status = (TextView) convertView.findViewById(R.id.tv_status);
            viewHolder.tv_type = (TextView) convertView.findViewById(R.id.tv_type);
            viewHolder.tv_date = (TextView) convertView.findViewById(R.id.tv_date);
            viewHolder.tvLeftRect = (TextView) convertView.findViewById(R.id.tv_left_rect);
            convertView.setTag(viewHolder);            //表示给View添加一个格外的数据，
        } else {
            viewHolder = (ViewHolder) convertView.getTag();//通过getTag的方法将数据取出来
        }
        String type = couponBean.getStatus();
        if(type.equals(STATE_PENGDING)){
            viewHolder.tvLeftRect.setBackgroundColor(Color.parseColor("#AACF53"));
            viewHolder.tv_status.setText("待评审");
        }else if(type.equals(STATUS_ON_SHELF)){
            viewHolder.tvLeftRect.setBackgroundColor(Color.parseColor("#FF8346"));
            viewHolder.tv_status.setText("已上架");
        }else if(type.equals(STATUS_OFF_SHELF)){
            viewHolder.tvLeftRect.setBackgroundColor(Color.parseColor("#CCCCCC"));
            viewHolder.tv_status.setText("已下架");
        }else if(type.equals(STATUS_REJECT)){
            viewHolder.tvLeftRect.setBackgroundColor(Color.parseColor("#CCCCCC"));
            viewHolder.tv_status.setText("驳回");
        }else{

        }
        viewHolder.tv_rule.setText(couponBean.getTitle() + "");
        String s_issued_num=couponBean.getIssued_num();
        if(TextUtils.isEmpty(s_issued_num)){
            s_issued_num="0";
        }
        viewHolder.tv_issued_num.setText(s_issued_num);
        String s_used_num=couponBean.getUsed_num();
        if(TextUtils.isEmpty(s_used_num)){
            s_used_num="0";
        }
        viewHolder.tv_use_num.setText(s_used_num);
        String s_verified_num=couponBean.getVerified_num();
        if(TextUtils.isEmpty(s_verified_num)){
            s_verified_num="0";
        }
        viewHolder.tv_verificatnum.setText(s_verified_num);
        String s_category_id="";
        switch (couponBean.getCategory_id()){
            case SettingData.Post_AddCoupons1:
                s_category_id="现金购买型";
                break;
            case SettingData.Post_AddCoupons2:
                s_category_id="礼券兑换型";
                break;
            case SettingData.Post_AddCoupons3:
                s_category_id="免费领取型";
                break;
        }
        viewHolder.tv_type.setText(s_category_id);
        String date="";
        try {
          date = couponBean.getExpired_date().split(" ")[0];
        }catch (Exception e){

        }
        viewHolder.tv_date.setText("截止日期："+date);

        return convertView;
    }

    public class ViewHolder {
        public TextView tvLeftRect;         //左边矩形条
        public TextView tv_rule;            //规则
        public TextView tv_issued_num;        //发行
        public TextView tv_use_num;            //领取
        public TextView tv_verificatnum;    //验证
        public TextView tv_status;            //状态
        public TextView tv_type;            //类型
        public TextView tv_date;            //日期

    }
}
